package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/8/18 14:40
 * @Version 1.0
 */
@Service
public class BaseCategoryTrademarkServiceImpl implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Override
    public void remove(Long category3Id, Long trademarkId) {
        //  删除本质 -- 逻辑删除更新数据
        LambdaQueryWrapper<BaseCategoryTrademark> trademarkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trademarkLambdaQueryWrapper.eq(BaseCategoryTrademark::getCategory3Id,category3Id).eq(BaseCategoryTrademark::getTrademarkId,trademarkId);
        this.baseCategoryTrademarkMapper.delete(trademarkLambdaQueryWrapper);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  base_category_trademark
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            //  循环遍历。
            trademarkIdList.forEach(tmId->{
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);
                //  循环插入数据.
                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
            });
        }
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //  本质：A 的独有 2,5,6 sql:
        //  A：1,2,3,5,6  B: 1,3
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(new LambdaQueryWrapper<BaseCategoryTrademark>().eq(BaseCategoryTrademark::getCategory3Id, category3Id));
        //  拉姆达表达式stream 流 复制小括号，写死右箭头，落地大括号
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  查询已绑定的品牌Id 1,3
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //  A 全集：
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);
            //  获取 2,5,6
            //            ArrayList<BaseTrademark> baseTrademarkArrayList = new ArrayList<>();
            //            if (!CollectionUtils.isEmpty(baseTrademarkList)){
            //                baseTrademarkList.forEach(baseTrademark -> {
            //                    if (!tmIdList.contains(baseTrademark.getId())) {
            //                        baseTrademarkArrayList.add(baseTrademark);
            //                    }
            //                });
            //            }
            if (!CollectionUtils.isEmpty(baseTrademarkList)){
                //  map 映射关系：
                List<BaseTrademark> baseTrademarkArrayList = baseTrademarkList.stream().filter(baseTrademark -> {
                    //  获取差集
                    return !tmIdList.contains(baseTrademark.getId());
                }).collect(Collectors.toList());
                return baseTrademarkArrayList;
            }
        }
        //  查询所有品牌集合数据
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //  根据三级分类Id 查询品牌Id
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(new LambdaQueryWrapper<BaseCategoryTrademark>().eq(BaseCategoryTrademark::getCategory3Id, category3Id));
        //  拉姆达表达式stream 流 复制小括号，写死右箭头，落地大括号
        //        List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
        //            return baseCategoryTrademark.getTrademarkId();
        //        }).collect(Collectors.toList());
        //        List<Long> tmIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> baseCategoryTrademark.getTrademarkId()).collect(Collectors.toList());
        //  使用方法引用获取数据. 1,3 select trademark_id from base_category_trademark where category3_id = 61 and is_deleted = 0
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  查询已绑定的品牌Id
            List<Long> tmIdList = baseCategoryTrademarkList.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //  根据品牌Id 获取到品牌列表集合
            return baseTrademarkMapper.selectBatchIds(tmIdList);
        }
        return null;
    }
}
